/**
 * @author Shen Yi - bm2736892@gmail.com
 */

function save(name)
{
	var mainConfig = new Object();
	saveLight(mainConfig);
	saveMaterial(mainConfig);
	savePrimitive(mainConfig);
	$.post("action/saveMod.php", {
		"config": toJSON(mainConfig),
		"modName": name,
	}, function(){
		saveInfo("保存成功");
	})
}

function saveLight(mainConfig)
{
	saveInfo("保存灯光中......");
	
	var lights = SceneManager.getLight();
	mainConfig.light = new Object();

	for(var lightName in lights){
		var light = lights[lightName];
		var mainConfigLight = mainConfig.light[lightName] = new Object();
		mainConfigLight.name = light.name;
		mainConfigLight.position = light.getPosition();
		mainConfigLight.color = light.getColor();
		mainConfigLight.type = light.getType();
		mainConfigLight.attenuation = light.getAttenuation();
		if(light.getType == "SPOT"){
			mainConfigLight.target = light.getTarget();
			mainConfigLight.aspect = light.getAspect();
		}
	}
}

function saveModel(mainConfig)
{
	saveInfo("保存模型中......");
	mainConfig.model = SceneManager.getModel();
}

function saveMaterial(mainConfig)
{
	saveInfo("保存材质中......");
	var materials = MaterialManager.getMaterial();
	mainConfig.material = new Object();

	for(var materialName in materials){
		if(getMaterialNameFromHighlight(materialName) == materialName){
			var option = MaterialManager.getOption(materialName);
			if (isset(option)) {
				mainConfig.material[materialName] = option;
			}
		}
	}
}

function savePrimitive(mainConfig)
{
	saveInfo("保存primitive中......");
	var transformPrimitiveMap = SceneManager.getTransformPrimitiveMap();
	mainConfig.primitive = new Object();
	for(var transformName in transformPrimitiveMap){
		if(transformName != "light"){
			var primitives = transformPrimitiveMap[transformName];
			for(var i = 0; i < primitives.length; i++){
				var primitive = SceneManager.getPrimitive(primitives[i]);
				if(primitive.material){
					mainConfig.primitive[primitives[i]] = primitive.material.name;
				}
			}
		}
	}
}

function saveInfo(info)
{
	$("#debug").html(info);
}
